package aupdg.logic.impl;

import java.util.ArrayList;
import java.util.List;

/**
 * Class that represents a data object used in an event.
 *
 * @author AA and CJ
 */
public class DataObjects {

    List<List<String>> dataObj = new ArrayList<>();

    /**
     * Constructor. Initializes the internal list.
     */
    public DataObjects() {
        this.dataObj.add(new ArrayList<String>()); //Data Object        
        this.dataObj.add(new ArrayList<String>()); //LastModifier (Output)
        this.dataObj.add(new ArrayList<String>()); //SecondToLastModifier (Output)
    }

    /**
     * Adds a data object with its last modifier.
     *
     * @param dataObjName
     * @param lastModifier
     * @param secondToLastModifier
     * @param type {I, O} Input or Output
     */
    public void addObject(String dataObjName, String lastModifier, String secondToLastModifier, String type) {
        String data;

        data = substr(dataObjName, type);

        this.dataObj.get(0).add(data);
        this.dataObj.get(1).add(lastModifier);
        this.dataObj.get(2).add(secondToLastModifier);
    }

    /**
     * Extracts the name of the data object.
     *
     * @param data
     * @param type
     * @return
     */
    public String substr(String data, String type) {
        if (type.equals("I")) {
            return data.substring(6);
        } else {
            return data.substring(7);
        }
    }

    /**
     * Returns true is the data object is already present in the list.
     *
     * @param dataObjName
     * @param type
     * @return
     */
    public Boolean existInDataObjects(String dataObjName, String type) {
        String data;

        data = substr(dataObjName, type);
        return this.dataObj.get(0).contains(data);
    }

    /**
     * To update the last modifier in case a new one is found.
     *
     * @param dataObjName
     * @param newModifier
     * @param type
     */
    public void updateLastModifier(String dataObjName, String newModifier, String type) {
        String data;
        data = substr(dataObjName, type);

        for (int i = 0; i < this.dataObj.get(0).size(); i++) {
            if (data.equals(this.dataObj.get(0).get(i))) {
                this.dataObj.get(2).set(i, this.dataObj.get(1).get(i)); //upd second to last modifier first
                this.dataObj.get(1).set(i, newModifier);
            }
        }
    }

    /**
     * Returns a data object's last modifier.
     *
     * @param dataObjName
     * @param type
     * @return
     */
    public String getLastModifier(String dataObjName, String type) {
        String data;
        data = substr(dataObjName, type);
        for (int i = 0; i < this.dataObj.get(0).size(); i++) {
            if (data.equals(this.dataObj.get(0).get(i))) {
                return this.dataObj.get(1).get(i);
            }
        }
        return "";
    }

    /**
     * Gets the second to last modifier. This method is used when an event has
     * the same input and output data object to avoid adding this data
     * dependence to the LDG. The second to last data-dependence (if it exists)
     * will be added instead.
     *
     * @param dataObjName
     * @param type
     * @return
     */
    public String getSecondToLastModifier(String dataObjName, String type) {
        String data;
        data = substr(dataObjName, type);
        for (int i = 0; i < this.dataObj.get(0).size(); i++) {
            if (data.equals(this.dataObj.get(0).get(i))) {
                return this.dataObj.get(2).get(i);
            }
        }
        return "";
    }
}
